package com.shujiaotong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shujiaotong.mapper.ClassesMapper;
import com.shujiaotong.pojo.Classes;
import com.shujiaotong.pojo.UserClass;
import com.shujiaotong.mapper.UserClassMapper;
import com.shujiaotong.service.IUserClassService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-10-20
 */
@Service
public class UserClassServiceImpl extends ServiceImpl<UserClassMapper, UserClass> implements IUserClassService {

    @Autowired
    private ClassesMapper classesMapper;

    @Override
    public long getClassesTotalCount(Long tid) {
        QueryWrapper<Classes> qw = new QueryWrapper<>();
        qw.lambda().eq(Classes::getTeacherId,tid);
        qw.select("count(*) as getCount");
        List<Map<String, Object>> maps = classesMapper.selectMaps(qw);
        return (long) maps.get(0).get("getCount");
    }

    @Override
    public long getMouthTotal(Long tid) {

        // 当前月初
        LocalDateTime time1 = LocalDateTime.of(LocalDateTime.now().getYear(),
                LocalDateTime.now().getMonth(),
                LocalDateTime.MIN.getDayOfMonth(),
                0, 0
        );
        // 当前月末
        LocalDateTime time2 = LocalDateTime.of(
                LocalDateTime.now().getYear(),
                LocalDateTime.now().getMonth(),
                LocalDateTime.now().getMonth().maxLength(),
                23, 59
        );

        // create time    2024-10-1   --- 2024-10-31
        // create time    2024-11-1   --- 2024-11-30
        QueryWrapper<Classes> qw = new QueryWrapper<>();
        qw.lambda().eq(Classes::getTeacherId,tid);
        qw.lambda().between(Classes::getCreateTime,time1,time2);
        qw.select("count(*) as getCount");
        List<Map<String, Object>> maps = classesMapper.selectMaps(qw);
        return (long) maps.get(0).get("getCount");
    }

    @Override
    public Map<String,Long> getTheacherCount(Long tid) {
// 通过线程池获取线程执行业务并返回线程执行结果
        CompletableFuture<Long> allcount = CompletableFuture.supplyAsync(() -> {
            return getClassesTotalCount(tid);
        });

        CompletableFuture<Long> mouthcount = CompletableFuture.supplyAsync(() -> {
            return getMouthTotal(tid);
        });
        // 等待两个线程执行完，在数据封装
        CompletableFuture.allOf(allcount,mouthcount).join();

        Map<String,Long> map = null;
        try {
            map = new HashMap<>();
            map.put("allCount",allcount.get());
            map.put("mouthCount",mouthcount.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return map;
    }
}
